package com.wit.dao.impl;

import java.util.List;

import com.wit.dao.ContactmanDao;
import com.wit.po.Client;
import com.wit.po.Contactman;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import hibernate.HibernateSessionFactory;

public class ContactmanDaoImpl implements ContactmanDao {

	
	public boolean addContactman(Contactman contact) {
		
		if(!isContactValid(contact))
			return false;
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			
			session.save(contact);
			
			tx.commit();
			flag = true;
			
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	/*
	
	public List<Contactman> getContactList(Contactman contact, Integer pageNumber, Integer pageSize) {

			
		String hql = "from Contactman contact limit ?,?";
		Session session = HibernateSessionFactory.getSession();
		Query query = session.createQuery(hql);
		
		query.setInteger(0, (pageNumber-1)*pageSize);
		query.setInteger(1, pageSize);
		
		List<Contactman> contactList = (List<Contactman>)query.list();

		session.close();
		return contactList;
	}
	*/
	
	public int getTotalAmount(Contactman contact)
	{
		Session session = null;
		Integer totalAmount = 0;
		try {
			session = HibernateSessionFactory.getSession();
	
			Criteria criteria = session.createCriteria(Contactman.class);
			if(contact != null)
			{
				String name = contact.getContactName();
				String clientId = null;
				String clientName = null;
				if(contact.getClient() != null)
				{
					clientId = contact.getClient().getClientId();
					clientName = contact.getClient().getClientName();
				}
				String phone = contact.getContactPhone();
				
				if(name != null && name.length() > 0)
					criteria.add(Property.forName("contactName").eq(name));
				if(clientId != null && clientId.length() > 0)
					criteria.add(Property.forName("client.clientId").eq(clientId));
				if(phone != null && phone.length() > 0)
					criteria.add(Property.forName("contactPhone").eq(phone));
				if(clientName != null && clientName.length() > 0)
					criteria = criteria.createCriteria("client").add(Restrictions.eq("clientName", clientName));
			}
			totalAmount = (Integer)criteria.setProjection(Projections.rowCount()).uniqueResult();
		
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return totalAmount;
	}
	
	public List<Contactman> getContactList(Contactman contact, Integer pageNumber, Integer pageSize)
	{
		Session session = null;
		List<Contactman> contactList = null;
		try {
			session = HibernateSessionFactory.getSession();
			
			Criteria criteria = session.createCriteria(Contactman.class);
			
			if(contact != null)
			{
				String name = contact.getContactName();
				String clientId = null;
				String clientName = null;
				
				if(contact.getClient() != null)
				{
					clientId = contact.getClient().getClientId();
					clientName = contact.getClient().getClientName();
				}
				String phone = contact.getContactPhone();
				
				if(name != null && name.length() > 0)
					criteria.add(Property.forName("contactName").eq(name));
				if(clientId != null && clientId.length() > 0)
					criteria.add(Property.forName("client.clientId").eq(clientId));
				if(phone != null && phone.length() > 0)
					criteria.add(Property.forName("contactPhone").eq(phone));
				if(clientName != null && clientName.length() > 0)
					criteria = criteria.createCriteria("client").add(Restrictions.eq("clientName", clientName));
			}
			criteria.setFirstResult((pageNumber-1)*pageSize);
			criteria.setMaxResults(pageSize);
			
			contactList = (List<Contactman>)criteria.list();
			
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return contactList;
	}
	
	
	public List<Contactman> getContactListByClientId(String clientId) {
		
		return s_GetContactListByClientId(clientId);
	}

	
	public Contactman getContactmanById(Integer contactId) {

		if(contactId == null)
			return null;
		String hql = "from Contactman contact where contact.contactId=?";
		Session session = null;
		Contactman contact = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			
			query.setInteger(0, contactId);
			contact = (Contactman)query.uniqueResult();
		
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return contact;
	}

	
	public boolean removeContactman(Contactman contact) {

		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			
			session.delete(contact);
			
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	
	public boolean updateContactman(Contactman contact) {

		if(!isContactValid(contact))
			return false;
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			
			session.update(contact);
			
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	
	public boolean isContactValid(Contactman contact)
	{
		if(ClientDaoImpl.s_GetClientById(contact.getClient().getClientId()) == null)
			return false;
		return true;
	}

	public static List<Contactman> s_GetContactListByClientId(String clientId) {

		if(clientId == null)
			return null;
		Session session = null;
		List<Contactman> contactList = null;
		try {
			String hql = "from Contactman contact where contact.client.clientId=?";
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			
			query.setString(0, clientId);
			
			contactList = (List<Contactman>)query.list();
		
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return contactList;
	}
	
	
}
